{% extends 'base.html' %}
{% block content %}
<div class="container mt-5">
    <h2>老师面板</h2>
    <!-- 添加查看题库分析的按钮 -->
    <a href="{{ url_for('show_analysis') }}" class="btn btn-primary mb-3">查看题库分析</a>
    <!-- 新增组卷按钮 -->
    <a href="{{ url_for('compose_paper') }}" class="btn btn-success mb-3 ml-2">组卷</a>
    <!-- 新增批量导出和导入按钮 -->
    <a href="{{ url_for('export_questions') }}" class="btn btn-secondary mb-3 ml-2">批量导出试题</a>
    <a href="#importModal" data-toggle="modal" class="btn btn-secondary mb-3 ml-2">批量导入试题</a>
    <a href="{{ url_for('view_papers') }}" class="btn btn-info mb-3 ml-2">查看试卷</a>
<!-- 导入模态框 -->
<div id="importModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">批量导入试题</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form method="POST" action="{{ url_for('import_questions') }}" enctype="multipart/form-data">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="file">选择Excel文件</label>
                        <input type="file" class="form-control" id="file" name="file" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">上传</button>
                </div>
            </form>
        </div>
    </div>
</div>

    <div class="row">
        <!-- 题目列表 -->
        <div class="col-md-8">
            <h4>题目列表</h4>
            <table class="table">
                <thead>
                    <tr>
                        <th>题目ID</th>
                        <th>课程ID</th>
                        <th>知识点ID</th>
                        <th>题型</th>
                        <th>内容</th>
                        <th>选项</th>
                        <th>答案</th>
                        <th>解释</th>
                        <th>难度</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for question in questions %}
                    <tr>
                        <td>{{ question[0] }}</td>
                        <td>{{ question[1] }}</td>
                        <td>{{ question[2] }}</td>
                        <td>{{ question[3] }}</td>
                        <td>{{ question[4] }}</td>
                        <td>{{ question[5] }}</td>
                        <td>{{ question[6] }}</td>
                        <td>{{ question[7] }}</td>
                        <td>{{ question[8] }}</td>
                        <td>
                            <button class="btn btn-primary btn-sm edit-question-btn" data-questionid="{{ question[0] }}" data-courseid="{{ question[1] }}" data-knowledgepointid="{{ question[2] }}" data-questiontype="{{ question[3] }}" data-content="{{ question[4] }}" data-options="{{ question[5] }}" data-answer="{{ question[6] }}" data-explanation="{{ question[7] }}" data-difficulty="{{ question[8] }}">编辑</button>
                            <form action="{{ url_for('delete_question_route', question_id=question[0]) }}" method="POST" style="display:inline;">
                                <button type="submit" class="btn btn-danger btn-sm">删除</button>
                            </form>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- 添加/编辑题目表单 -->
        <div class="col-md-4">
            <h4 id="form-title">添加题目</h4>
            <form method="POST" id="question-form" action="{{ url_for('add_question_route') }}">
                <div class="form-group">
                    <label for="QuestionID">题目ID</label>
                    <input type="text" class="form-control" id="QuestionID" name="QuestionID" required>
                </div>
                <div class="form-group">
                    <label for="CourseID">课程ID</label>
                    <input type="text" class="form-control" id="CourseID" name="CourseID" required>
                </div>
                <div class="form-group">
                    <label for="KnowledgePointID">知识点ID</label>
                    <input type="text" class="form-control" id="KnowledgePointID" name="KnowledgePointID" required>
                </div>
                <div class="form-group">
                    <label for="QuestionType">题型</label>
                    <input type="text" class="form-control" id="QuestionType" name="QuestionType" required>
                </div>
                <div class="form-group">
                    <label for="Content">内容</label>
                    <input type="text" class="form-control" id="Content" name="Content" required>
                </div>
                <div class="form-group">
                    <label for="Options">选项</label>
                    <input type="text" class="form-control" id="Options" name="Options">
                </div>
                <div class="form-group">
                    <label for="Answer">答案</label>
                    <input type="text" class="form-control" id="Answer" name="Answer" required>
                </div>
                <div class="form-group">
                    <label for="Explanation">解释</label>
                    <input type="text" class="form-control" id="Explanation" name="Explanation" required>
                </div>
                <div class="form-group">
                    <label for="Difficulty">难度</label>
                    <input type="text" class="form-control" id="Difficulty" name="Difficulty" required>
                </div>
                <button type="submit" class="btn btn-primary">提交</button>
                <button type="reset" class="btn btn-secondary" id="reset-btn">重置</button>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const editButtons = document.querySelectorAll('.edit-question-btn');
    const formTitle = document.getElementById('form-title');
    const questionForm = document.getElementById('question-form');
    const resetBtn = document.getElementById('reset-btn');

    editButtons.forEach(button => {
        button.addEventListener('click', function() {
            const questionId = this.getAttribute('data-questionid');
            const courseId = this.getAttribute('data-courseid');
            const knowledgePointId = this.getAttribute('data-knowledgepointid');
            const questionType = this.getAttribute('data-questiontype');
            const content = this.getAttribute('data-content');
            const options = this.getAttribute('data-options');
            const answer = this.getAttribute('data-answer');
            const explanation = this.getAttribute('data-explanation');
            const difficulty = this.getAttribute('data-difficulty');

            document.getElementById('QuestionID').value = questionId;
            document.getElementById('CourseID').value = courseId;
            document.getElementById('KnowledgePointID').value = knowledgePointId;
            document.getElementById('QuestionType').value = questionType;
            document.getElementById('Content').value = content;
            document.getElementById('Options').value = options;
            document.getElementById('Answer').value = answer;
            document.getElementById('Explanation').value = explanation;
            document.getElementById('Difficulty').value = difficulty;

            formTitle.textContent = '编辑题目';
            questionForm.action = `/teacher/edit_question/${questionId}`;
        });
    });

    resetBtn.addEventListener('click', function() {
        formTitle.textContent = '添加题目';
        questionForm.action = '{{ url_for("add_question_route") }}';
    });
});
</script>
{% endblock %}
